package com.zunipe.until;

import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.LiteralText;

public class AutoEatFunction
{
    public static void AutoEat(MinecraftClient client)
    {
        if (client.player == null)
            return;
        if (!Util.autoEat)
        {
            client.player.sendMessage(new LiteralText("§l自动吃东西 §2§l开启"), true);
            Util.autoEat = true;
            client.options.keyUse.setPressed(true);
            new Thread(() ->
            {
                while (Util.autoEat)
                {
                    if (!client.player.getMainHandStack().getItem().isFood())
                    {
                        PlayerInventory inventory = client.player.getInventory();
                        for (int i = 9; i < inventory.size(); ++i)
                        {
                            if (inventory.getStack(i).isFood())
                            {
                                client.interactionManager.clickSlot(client.player.playerScreenHandler.syncId, i, inventory.selectedSlot, SlotActionType.SWAP, client.player);
                                break;
                            }
                        }
                    }
                    client.options.keyUse.setPressed(true);
                    if (client.player.getHealth() <= 2.0f)
                        client.options.keySneak.setPressed(true);
                    else if (client.player.getHealth() > 15.0f)
                        client.options.keySneak.setPressed(false);
                }
            }).start();
        }
        else
        {
            client.player.sendMessage(new LiteralText("§l自动吃东西 §4§l关闭"), true);
            Util.autoEat = false;
            client.options.keySneak.setPressed(false);
            client.options.keyUse.setPressed(false);
        }
    }
}
